//
//  TableViewController.m
//  OCfrist
//
//  Created by 宋 on 15/11/9.
//  Copyright © 2015年 宋. All rights reserved.
//

#import "TableViewController.h"
#import "ViewController.h"
#import "TextTableViewController.h"
#import "Test2ViewController.h"
@interface TableViewController ()
{
    NSArray * data;
    NSArray * detail;
    NSArray * headers;
}


@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    data = @[
             @[@"安安",@"安排"],
             @[@"柏晓宇",@"白羽"],
             @[@"陈晓",@"陈斯"],
             @[@"戴晓",@"冬瓜"],
             @[@"恩恩",@"饿呢"],
             @[@"汪晓",@"汪思"],
             @[@"黄艳端",@"黄花"],
             @[@"张鑫",@"张飞"]
             ];
    detail = @[
               @"1",
               @"2",
               @"3"
               ];
    headers = @[
                @"A",
                @"B",
                @"C",
                @"D",
                @"E",
                @"W",
                @"H",
                @"Z"
                ];
    //导航栏功能delete
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"联系人";
}
/*- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}*/

//1.联系人分类
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    //截取数组值
    cell.textLabel.text = data[indexPath.section][indexPath.row];
    //cell.detailTextLabel.text = detail[indexPath.row];
    
    //选择标识
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    return cell;
}

//1.联系人分类(字母出现的地方)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return headers[section];
    
}

//点击选项背景变色
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TextTableViewController * con = [TextTableViewController new];
    [self.navigationController pushViewController:con animated:YES];
}
//拖动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{

}

//删除功能的实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray *arr = data.mutableCopy;
        /*[arr removeObjectAtIndex:indexPath.row];
         data = arr;
         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];*/
        
        //删除联系人
        NSMutableArray *arr1 = [data[indexPath.section] mutableCopy];
        [arr1 removeObjectAtIndex:indexPath.row];
        [arr replaceObjectAtIndex:indexPath.section withObject:arr1];
        data = arr;
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



@end
